﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Zhuang.WebSite.Areas.SystemManagement.Models;
using System.Data;
using Zhuang.BLL.SystemManagement;
using Zhuang.Model.EF;

namespace Zhuang.WebSite.Areas.SystemManagement.Controllers
{
    public class LoginController : Controller
    {
        //
        // GET: /SystemManagement/Login/

        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Login()
        {
            string result = "";

            string username = Request.Params["username"]??"";
            string password = Request.Params["password"]??"";


            Sys_UserBLL user = new Sys_UserBLL();

            Sys_User userEntity = new Sys_User();

            DataTable dt= user.GetLoginUser(username,password);

            if (dt.Columns[0].ColumnName == "failMsg")
            {
                result = dt.Rows[0][0].ToString();
            }
            else
            {
                result = "";

                //Zhuang.Utility.Common.Entity.EntityHelper<User.Entity> entityHelper=new Zhuang.Utility.Common.Entity.EntityHelper<User.Entity>();
                userEntity = Zhuang.Utility.Common.Entity.EntityHelper<Sys_User>.FillEntity(dt.Rows[0]);

                AppCode.Common.SessionHelper.SetUser(userEntity) ;

                AppCode.Common.SessionHelper.SetUserMenus(user.GetUserMenus(userEntity.UserName));



            }


            return Json(result);
        }

        public ActionResult Logout()
        {
            Session["userEntity"] = null;

            return RedirectToAction("Index");

        }
    }
}
